var qcloud = require('../../..//vendor/sdk/index');
var config = require('../../..//config');
let app = getApp();
Page({
  data: {
    navH: app.globalData.navHeight,
    userInfo: null,
    errorMsg: '输入错误',
    showTopTips: false,
    formItems: [
      { label: '手机', name: 'mobile', placeholder: '请输入手机', is: 'VInput', getCodeBtnDisable: false, getCodeText: '获取验证码'},
      { label: '验证码', name: 'validateCode', placeholder: '请输入验证码' }
    ]
  },
  onLoad() {
    app.getUserInfo().then(userInfo => {
      this.setData({ userInfo });
    });
  },
  onNavBack() {
    // 返回
    wx.navigateBack();
  },
  validateData(fields, data) {
    let v = {};
    typeof data != 'object' ? (v[fields] = data) : (v = data);
    fields = [].concat(fields);
    let result = [];

    let msgs = [];
    let emailReg = /^\w+@\w{2,10}\.\w{2,5}$/;
    let mobileReg = /^1\d{10}$/;
    let re = new RegExp(mobileReg);
    fields.forEach(field => {
      if (field == 'mobile') {
        if (!v.mobile) {
          result.push('mobile');
          msgs.push('请输入手机');
        }
        if (!re.test(v.mobile)) {
          result.push('mobile');
          msgs.push('请输入合法的手机格式');
        }
      }
      if (field == 'validateCode') {
        if (!v.validateCode) {
          result.push('validateCode');
          msgs.push('请输入验证码');
        }
        if (v.validateCode != this.data.mobileCode) {
          result.push('validateCode');
          msgs.push('验证码不正确');
        }
      }
    });
    this.showError(result);
    return result.length == 0;
  },
  setMobile(e) {
    this.setData({ mobile: e.detail.value });
    this.changeWran(e);
  },
  changeWran(e) {
    let id = e.currentTarget.id;
    let formItems = this.data.formItems;
    formItems.forEach(v => {
      if (id == v.name) {
        v.warn = false;
      }
    });
    this.setData({
      showTopTips: false,
      formItems
    });
  },
  showError(field, msg) {
    field = [].concat(field);
    let formItems = this.data.formItems;
    formItems.forEach(v => {
      if (field.includes(v.name)) {
        v.warn = true;
      } else {
        v.warn = false;
      }
    });

    if (field.length > 0) {
      var that = this;
      this.setData({
        showTopTips: true,
        errorMsg: msg || '输入错误',
        formItems
      });
      setTimeout(function() {
        that.setData({
          showTopTips: false
        });
      }, 2000);
    } else {
      this.setData({
        formItems
      });
    }
  },
  getMobileCode() {
    let isPass = this.validateData('mobile', this.data.mobile);
    if (!isPass) {
      return;
    }

    this._countdown();

    let self = this;
    qcloud.request({
      url: config.service.getMobileCodeUrl,
      data: {
        mobile: this.data.mobile
      },
      success: res => {
        let data = res.data.data;
        self.setData({ mobileCode: data });
      }
    });
  },
  _countdown() {
    let self = this;
    let seconde = 60;

    let formItems = this.data.formItems;
    let index = formItems.findIndex(v => v.name == 'mobile');

    self.interval = setInterval(() => {
      if (seconde <= 0) {
        formItems[index].getCodeText = `获取验证码`;
        formItems[index].getCodeBtnDisable = false;
        self.setData({ formItems });
        clearInterval(self.interval);
      } else {
        formItems[index].getCodeText = `获取验证码(${seconde})`;
        formItems[index].getCodeBtnDisable = true;
        self.setData({ formItems });
        seconde--;
      }
    }, 1000);
  },
  formSubmit(e) {
    var that = this;
    var formData = e.detail.value;
    var data = { ...formData, id: this.data.userInfo.id };
    // var data = {...formData}

    let isPass = this.validateData(
      [
        'realName',
        'companyName',
        'job',
        'address',
        'email',
        'mobile',
        'validateCode'
      ],
      data
    );
    if (!isPass) return;

    qcloud.request({
      url: config.service.regToPublisherUlr,
      method: 'post',
      data,
      success(res) {
        app.refreshUserInfo(() => {
          wx.navigateTo({
            url: '../personalPage'
          });
        });
      }
    });
  }
});
